﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Murry.DataStore.MemoryDB.UnitTests
{
    /// <summary>
    /// Interface for DerivedMemoryPersist object.
    /// </summary>
    public interface IDerivedMemoryPersist : IPersist
    {
        /// <summary>
        /// Test string property.
        /// </summary>
        string A { get; set; }

        /// <summary>
        /// Test int property.
        /// </summary>
        int B { get; set; }

    }

    /// <summary>
    /// Derive an test object from MemoryPersist.
    /// </summary>
    [Serializable]
    public class DerivedMemoryPersist : MemoryPersist, IDerivedMemoryPersist
    {
        /// <summary>
        /// Standard constructor.
        /// </summary>
        /// <param name="database">In-Memory Database.</param>
        public DerivedMemoryPersist(MemoryDatabase database)
            : base(database) { }

        /// <summary>
        /// Test string property.
        /// </summary>
        public string A { get; set; }

        /// <summary>
        /// Test int property.
        /// </summary>
        public int B { get; set; }

        /// <summary>
        /// Expose Id set property.
        /// </summary>
        public new string Id 
        {
            get { return base.Id; }
            set { base.Id = value; }
        }

        /// <summary>
        /// Get access to the protected Database property.
        /// </summary>
        public new MemoryDatabase Database
        {
            get { return base.Database; }
        }

        /// <summary>
        /// Implement a find by filter, for testing.
        /// </summary>
        /// <param name="database">Database.</param>
        /// <param name="filter">Filter.</param>
        /// <returns>Filtered results.</returns>
        public static IEnumerable<IDerivedMemoryPersist> FindByFilter(MemoryDatabase database, MockFilter filter )
        {
            lock (database)
            {
                return from o in database.GetQueryable<IDerivedMemoryPersist>(typeof(DerivedMemoryPersist)) 
                       where filter.A == o.A select o;
            }
        }
    }

    /// <summary>
    /// Create an interface that is just not supported.
    /// </summary>
    public interface INotSupportedMemoryPersist : IDerivedMemoryPersist
    {
    }

    /// <summary>
    /// Mock filter to test query by filter
    /// </summary>
    public class MockFilter
    {
        /// <summary>
        /// Just a string.
        /// </summary>
        public string A { get; set; }
    }
}
